package com.block.chlunion.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.chlunion.enums.UnionActionEnum;
import com.block.chlunion.enums.UnionStatusEnum;
import com.block.chlunion.model.UnionLoginDTO;
import com.block.chlunion.model.param.UnionCheckParam;
import com.block.chlunion.model.param.UnionLoginParam;
import com.block.chlunion.service.IChannleUnionAddupService;
import com.block.db.entity.ChannelInfo;
import com.block.db.entity.ChannelUnionResultInfo;
import com.block.db.mapper.ChannelUnionResultInfoMapper;
import com.block.chlunion.service.IChannelUnionResultInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 渠道联登结果信息 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-06-08
 */
@Service
public class ChannelUnionResultInfoServiceImpl extends ServiceImpl<ChannelUnionResultInfoMapper, ChannelUnionResultInfo> implements IChannelUnionResultInfoService {

    @Autowired
    IChannleUnionAddupService channleUnionAddupService;

    @Override
    public ChannelUnionResultInfo saveResultInfo(ChannelInfo channelInfo, UnionCheckParam unionCheckParam){

        ChannelUnionResultInfo newEntity = new ChannelUnionResultInfo();
        newEntity.setDateInfo(unionCheckParam.getDateInfo());
        newEntity.setChannelCode(channelInfo.getChannelCode());
        newEntity.setChannelName(channelInfo.getChannelName());
        newEntity.setMobileMd5(unionCheckParam.getMobileMd5());
        newEntity.setOrderNo(unionCheckParam.getOrderNo());
        newEntity.setReqIp(unionCheckParam.getReqIp());
        boolean save = this.save(newEntity);

        if (!save){
            return null;
        }

        return this.getById(newEntity.getId());
    }

    @Override
    public boolean updateResultInfo(ChannelInfo channelInfo, UnionLoginDTO unionLoginDTO){

        if (ObjectUtil.equals(unionLoginDTO.getUnionStatusEnum().getValue(), UnionStatusEnum.SUCCESS.getValue())){
            channleUnionAddupService.saveOrUpdateDataAddupInfo(unionLoginDTO.getDateInfo(), UnionActionEnum.SUCCESS, channelInfo);
        }else{
            channleUnionAddupService.saveOrUpdateDataAddupInfo(unionLoginDTO.getDateInfo(), UnionActionEnum.REGISTER, channelInfo);
        }

        ChannelUnionResultInfo updateEntity = new ChannelUnionResultInfo();
        updateEntity.setId(Long.valueOf(unionLoginDTO.getResultInfoId()));
        updateEntity.setStatus(unionLoginDTO.getUnionStatusEnum().getValue());
        updateEntity.setResultInfo(unionLoginDTO.getResultMsg());
        updateEntity.setUid(unionLoginDTO.getUid());
        boolean update = this.updateById(updateEntity);
        return update;
    }


}
